package cn.itcast.work.service.commit_service;


import cn.itcast.work.dao.commit.CommitSearch;
//import cn.itcast.work.datasource.DataConnection;
import cn.itcast.work.pojo.Commit;
import cn.itcast.work.pojo.Recover;
import cn.itcast.work.pojo.Text;
import cn.itcast.work.pojo.User;


import cn.itcast.work.service.user_service.UserService;
//import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * * @author: Mao Wanting
 * * @date: 2024/6/11
 * *description:评论服务层
 **/
@Service("CommitService")
public class CommitService {

    @Autowired
    private CommitSearch commitSearch;

    @Autowired
    private UserService userService;

    /**
     * 添加评论
     *
     * @param user
     * @param text
     * @param commit
     * @return
     * @throws IOException
     */
    @Transactional
    public String addCommit(User user, Text text, Commit commit) {
        int result = commitSearch.addCommit(user, text, commit);
        if (result <= 0) {
            return "评论失败";
        }

        return "成功评论";
    }

    /**
     * 添加回复
     *
     * @param user
     * @param commit
     * @param recover
     * @return
     * @throws IOException
     */
    @Transactional
    public String addRecover(User user, Commit commit, Recover recover) {
        int result = commitSearch.addRecover(user, commit, recover);
        if (result <= 0) {
            return "回复失败";
        }
        return "成功回复";
    }

    /**
     * 删除评论
     *
     * @param user
     * @param commit
     * @return
     * @throws IOException
     */
    @Transactional
    public String deleteCommit(User user, Commit commit) {


        //首先查出评论下有什么回复
        List<Recover> recovers = commitSearch.selectAllRecover(commit);
        //然后删除先删除回复
        for (Recover recover : recovers) {
            DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            String timeString = df.format(recover.getTime());
            recover.setTimeString(timeString);//设置每一个评论的时间字符串信息
            commitSearch.deleteRecover(user, commit, recover);
        }
        //然后删除评论
        int result = commitSearch.deleteCommit(user, commit);
        if (result <= 0) {
            return "删除评论失败";
        }

        return "删除评论成功";
    }

    /**
     * 删除回复
     *
     * @param user
     * @param commit
     * @param recover
     * @return
     * @throws IOException
     */
    @Transactional
    public String deleteRecover(User user, Commit commit, Recover recover) {

        int result_recover = commitSearch.deleteRecover(user, commit, recover);
        if (result_recover <= 0) {
            return "删除失败";
        }

        return "成功删除";
    }

    /**
     * @param text
     * @return 返回所有评论回复集合
     */
    @Transactional
    public Map<Commit, List<Recover>> selectCommitAndRecover(Text text) {

        Map<Commit, List<Recover>> stringListMap = new HashMap<>();
        List<Commit> commits = commitSearch.selectAllCommit(text.getId());    //查评论，查出评论的4个信息

        //遍历每一个评论
        for (Commit commit : commits) {
            DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            String timeString = df.format(commit.getTime());
            commit.setTimeString(timeString);//设置每一个评论的时间字符串信息

            User user = commitSearch.selectUserByCommit(commit);//通过commit的信息查出user的信息

//            System.out.println("查出的评论的用户信息" + user);
            commit.setUser(user);
            commit.setUserPath(userService.toSelectPic(user.getPhone()));

            List<Recover> recovers = commitSearch.selectAllRecover(commit);//通过评论得到所有的recover的信息

            //得到时间字符串
            for (Recover recover : recovers) {
                recover.setTimeString(df.format(recover.getTime()));//设置回复的时间字符串
                User user1 = commitSearch.selectUserByRecover(recover);//通过recover的每个信息查出用户信息
                recover.setUser(user1);//保存用户信息
                recover.setUserPath(userService.toSelectPic(user1.getPhone()));
//                System.out.println("查出的回复的用户信息为" + user1);
            }
            stringListMap.put(commit, recovers);
        }
//        System.out.println("查询该文章的评论回复的map集合如下=======");
//        System.out.println(stringListMap);
        return stringListMap;
    }

    //返回text的id
    @Transactional
    public int selectTextIdByTitle(Text text) {
        int result = commitSearch.selectTextIdByTitle(text);
        return result;
    }


    //强制删除评论
    public String DeleteCommit(Commit commit) {
        List<Recover> recovers = commitSearch.selectAllRecover(commit);
        for (Recover recover : recovers) {
            DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            recover.setTimeString(df.format(recover.getTime()));
            commitSearch.DeleteRecover(recover, commit);
        }

        int result = commitSearch.DeleteCommit(commit);

        return result > 0 ? "删评成功" : "删评失败";


    }

    @Transactional
    //强制删除回复
    public String DeleteRecover(Recover recover, Commit commit) {
        DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        recover.setTimeString(df.format(recover.getTime()));
        int result = commitSearch.DeleteRecover(recover, commit);
        if (result > 0) {
            return "删评成功";
        } else {
            return "删评失败";
        }
    }
}
